package tom.client.stock;

import java.util.Map;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.client.MainMenu;
import tom.client.MainPanel;
import tom.client.invoice.StockPopup;
import tom.shared.InvItem;
import tom.shared.Invoice;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;

public class FormStockAdjust extends DataForm {

	private static FormStockAdjust panel;

	public static FormStockAdjust getPanel() {

		if (panel == null)
			panel = GWT.create(FormStockAdjust.class);
		return panel;
	}

	protected Invoice record;

	private final ListDataProvider<InvItem> itemData = new ListDataProvider<InvItem>();
	private final CellTable<InvItem> cellTable;
	private final SingleSelectionModel<InvItem> itemSelection = new SingleSelectionModel<InvItem>();

	// private SingleSelectionModel<InvItem> itemSelection = new
	// SingleSelectionModel<InvItem>();

	public FormStockAdjust() {
		setSize("800px", "500px");
		lbCode.setText("เลขที่ใบส่งของ");

		Label lblNewLabel = new Label("ลูกค้า");
		add(lblNewLabel, 23, 111);

		txtCustomer = new TextBox();
		txtCustomer.setReadOnly(true);
		add(txtCustomer, 122, 111);
		txtCustomer.setSize("384px", "16px");

		txtInvDate = new TextBox();
		add(txtInvDate, 384, 74);
		txtInvDate.setSize("122px", "16px");

		Label label = new Label("วันที่");
		add(label, 340, 74);
		label.setSize("29px", "21px");

		ScrollPanel scrollPanel = new ScrollPanel();
		add(scrollPanel, 22, 159);
		scrollPanel.setSize("760px", "330px");

		cellTable = new CellTable<InvItem>();
		cellTable.setStyleName("table");
		scrollPanel.setWidget(cellTable);
		cellTable.setPageSize(10);
		cellTable.setTableLayoutFixed(true);
		cellTable.setAccessKey('x');
		cellTable.setSize("100%", "100%");
		itemData.addDataDisplay(cellTable);
		cellTable.setSelectionModel(itemSelection);

		TextColumn<InvItem> colProCode = new TextColumn<InvItem>() {
			@Override
			public String getValue(InvItem object) {
				return object.proCode;
			}
		};
		colProCode.setCellStyleNames("table");
		cellTable.addColumn(colProCode, "Code");
		cellTable.setColumnWidth(colProCode, "120px");

		TextColumn<InvItem> colProDescript = new TextColumn<InvItem>() {
			@Override
			public String getValue(InvItem object) {
				return object.proName;
			}
		};
		colProDescript.setCellStyleNames("table");
		cellTable.addColumn(colProDescript, "Name");
		cellTable.setColumnWidth(colProDescript, "250px");

		TextColumn<InvItem> colQty = new TextColumn<InvItem>() {
			@Override
			public String getValue(InvItem object) {
				return String.valueOf(object.qty);
			}
		};
		colQty.setCellStyleNames("table");
		colQty.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		cellTable.addColumn(colQty, "QTY");
		cellTable.setColumnWidth(colQty, "60px");

		// TextColumn<InvItem> colStock = new TextColumn<InvItem>() {
		// @Override
		// public String getValue(InvItem object) {
		// return object.stkCode;
		// }
		// };
		// colStock.setCellStyleNames("table");
		// colStock.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		//
		// cellTable.addColumn(colStock, "Stock");
		// cellTable.setColumnWidth(colStock, "60px");

		//StockPopup.popup.setTable(cellTable);
		EditTextCell cell = new EditTextCell();
		Column<InvItem, String> colStock = new Column<InvItem, String>(cell) {
			@Override
			public String getValue(InvItem object) {
				return object.stkCode;
			}
		};
		colStock.setCellStyleNames("table");
		colStock.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		colStock.setFieldUpdater(new FieldUpdater<InvItem, String>() {
			@Override
			public void update(int index, InvItem object, String value) {
				object.stkCode = value;
				FormStockAdjust.this.setMode(2); // Set update mode
				//StockPopup.popup.open(object);
				//Window.alert("Clickable TextCell");
			}
		});
		cellTable.addColumn(colStock, "Stk.Loc.");
		cellTable.setColumnWidth(colStock, "40px");

		// TextColumn<InvItem> colVat = new TextColumn<InvItem>() {
		// @Override
		// public String getValue(InvItem object) {
		// return Library.moneyFormat(object.vatAmt);
		// }
		// };
		// colVat.setCellStyleNames("table");
		// colVat.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		// cellTable.addColumn(colVat, "VAT");
		// cellTable.setColumnWidth(colVat, "80px");

		// TextColumn<InvItem> colAmount = new TextColumn<InvItem>() {
		// @Override
		// public String getValue(InvItem object) {
		// return Library.moneyFormat(object.total);
		// }
		// };
		// colAmount.setCellStyleNames("table");
		// colAmount.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		// cellTable.addColumn(colAmount, "Amount");
		// cellTable.setColumnWidth(colAmount, "100px");

		Button button = new Button("?");
		button.setText("?");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				LookupDialog.show("เลือกใบส่งของจากวันที่", "invSaleDate",
						txtCode);
			}
		});
		add(button, 521, 74);
		button.setSize("25px", "25px");

		btnStock = new Button("New button");
		btnStock.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				MainPanel.mainPanel.show(FormStockLoc.getPanel(), "ปรับสต็อค");
				FormStockLoc.getPanel().open(
						itemSelection.getSelectedObject().proCode);
				FormStockLoc.getPanel().setInvNo(txtCode.getText());
				// Window.alert(itemSelection.getSelectedObject().proCode);
			}
		});
		btnStock.setText("แก้ไขสต็อค");
		add(btnStock, 692, 129);

	}

	@Override
	protected void search() {
		LookupDialog.show("เลือกใบส่งของจากชื่อลูกค้า", "invNo", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getSaleService().selectInvoice(
				Integer.valueOf(txtCode.getText()), dataCallback);

	}

	@Override
	protected void update() {
		Library.getSaleService().updateInvoice(record, updateCallback);
	}

	@Override
	protected void insert() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void delete() {

	}

	@Override
	protected void clearFields() {
		super.clearFields();
		txtCustomer.setText(null);
		txtInvDate.setText(null);
		itemData.getList().clear();
		itemData.refresh();
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtCustomer.setText(record.contactAddr);
			// txtDescript.setText(record.descript);
			txtInvDate.setText(Library.DATE_FORMAT.format(record.invDate));
			itemData.setList(record.items);
		} else
			clearFields();
	}

	@Override
	protected void getFields() {
		// TODO Auto-generated method stub

	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	AsyncCallback<Invoice> dataCallback = new AsyncCallback<Invoice>() {

		@Override
		public void onSuccess(Invoice result) {
			record = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};
	
	AsyncCallback<String> updateCallback = new AsyncCallback<String>() {

		@Override
		public void onSuccess(String result) {
			Window.alert("Update INV#" + result);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	private final TextBox txtCustomer;

	private final TextBox txtInvDate;

	private final Button btnStock;
}
